ADB over TCP inside WSL2
2021-12-11 ยท 2 min read
Problem #
There are two problems with WSL2 that make connecting to an Android device over adb challenging. First, the network interface inside WSL2 is behind a NAT and not exposed to the LAN. This means we can't directly connect to any devices (like our phone) on the LAN. Second, any connected USB devices aren't visible inside WSL2.
Possible Approaches #
(Chosen) Create a
socattunnel inside Windows pointing at the phone.Change the WSL2 virtual network from NAT to Bridge mode (looks sketchy)
Hack some stuff to make USB devices forward to WSL2? (looks sketchy)
https://devblogs.microsoft.com/commandline/connecting-usb-devices-to-wsl/
Overview #
Since we can't connect directly from WSL2 to our device, we're going to set up a TCP tunnel running on the Windows side, pointing to the device. We'll then run all our adb operations over this tunnel.
Process #
Install
socatinside MSYS2 in Windows.msys$ pacman -S socatOn the Android device, enable "Wireless debugging" inside the Developer options.
In the "Wireless debugging" menu, hit "Pair device with pairing code". This will generate a random code and bind the phone
adbdaemon to a random port. This port is only used temporarily for the pairing process.Inside MSYS2, setup a temporary tunnel pointing to the device IP and port for the WSL2
adbto pair on.msys$ socat tcp4-listen:2205,reuseaddr,fork,nodelay tcp4-connect:192.168.2.242:43148,nodelayInside WSL2, pair with the android device over our temporary
socattunnel.wsl2$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.19.160.252 netmask 255.255.240.0 broadcast 172.19.175.255 wsl2$ adb pair 172.19.160.1:2205Close the temporary
socattunnel.Note the long-term wireless debugging port, in this case 38654.
Inside MSYS2, setup a new tunnel pointing at the device IP and long-term port. Keep in mind that we'll always need this tunnel running whenever we want to use
adbinside WSL2.msys$ socat tcp4-listen:2205,reuseaddr,fork,nodelay tcp4-connect:192.168.2.242:38654,nodelayInside WSL2, connect to the android device over our tunnel.
wsl2$ adb connect 172.19.160.1.:2205Make sure it works.
wsl2$ adb shell walleye:/ $